﻿namespace System.Text
{
	internal sealed class CP866UDecoder : Decoder
	{
		private readonly Decoder _decoder = Encoding.GetEncoding(866).GetDecoder();

		public override int GetCharCount(byte[] bytes, int byteIndex, int byteCount)
		{
			return _decoder.GetCharCount(bytes, byteIndex, byteCount);
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "3")]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
		public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
		{
			var count = _decoder.GetChars(bytes, byteIndex, byteCount, chars, charIndex);

			for (var i = 0; i < count; i++)
			{
				switch (bytes[byteIndex + i])
				{
					case 0xF2: chars[charIndex + i] = (char)0x490; break; //Ґ
					case 0xF3: chars[charIndex + i] = (char)0x491; break; //ґ
					case 0xF4: chars[charIndex + i] = (char)0x404; break; //Є
					case 0xF5: chars[charIndex + i] = (char)0x454; break; //є
					case 0xF6: chars[charIndex + i] = (char)0x406; break; //І
					case 0xF7: chars[charIndex + i] = (char)0x456; break; //і
					case 0xF8: chars[charIndex + i] = (char)0x407; break; //Ї
					case 0xF9: chars[charIndex + i] = (char)0x457; break; //ї
				}
			}

			return count;
		}
	}
}